package com.huo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huo.constant.RedisConstant;
import com.huo.dao.SetmealDao;
import com.huo.entity.PageResult;
import com.huo.entity.QueryPageBean;
import com.huo.pojo.Setmeal;
import com.huo.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;


@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;

    /**
     * @Description: 添加套餐
     * @Author: 汪青松
     * @Param: [checkgroupIds, setmeal]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/22 17:02
     */
    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        setmealDao.add(setmeal);
        setSetmealIdAndcheckgroupIds(checkgroupIds, setmeal.getId());
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
    }

    /**
     * @Description: 分页
     * @Author: 汪青松
     * @Param: [queryPageBean]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/24 18:13
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> setmealPage = setmealDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    /**
     * @Description: 获取套餐列表
     * @Author: 汪青松
     * @Param: []
     * @Return: java.util.List<com.huo.pojo.Setmeal>
     * @CreateDate: 2021/2/10 22:27
     */
    @Override
    public List<Setmeal> getAllSetmeal() {
        return setmealDao.getAllSetmeal();
    }

    /**
     * @Description: 根据id查找套餐信息
     * @Author: 汪青松
     * @Param: [id]
     * @Return: com.huo.pojo.Setmeal
     * @CreateDate: 2021/2/15 13:50getAllSetmeal
     */
    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    /**
     * @Description: 设置检查组id和套餐id的关系
     * @Author: 汪青松
     * @Param: [checkgroupIds, id]s
     * @Return: void
     * @CreateDate: 2021/1/22 17:11
     */
    private void setSetmealIdAndcheckgroupIds(Integer[] checkgroupIds, Integer id) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.setSetmealIdAndcheckgroupIds(id, checkgroupId);
            }
        }
    }

}
